package com.example.baserecyclerviewadapterdemo.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.adapter.AnimationAdapter;
import com.example.baserecyclerviewadapterdemo.adapter.ItemClickAdapter;
import com.example.baserecyclerviewadapterdemo.entity.ClickEntity;

import java.util.ArrayList;
import java.util.List;

public class ItemClickActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ItemClickAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_universal_recycler);

        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("ItemClickActivity");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initAdapter();
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Toast.makeText(ItemClickActivity.this, "You Click position:" + position, Toast.LENGTH_SHORT).show();
            }
        });
        mAdapter.addChildClickViewIds(R.id.btn);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()) {
                    case R.id.btn:
                        Toast.makeText(ItemClickActivity.this, "您点击了按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    private void initAdapter() {
        List<ClickEntity> data = new ArrayList<>();
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_VIEW));
        data.add(new ClickEntity(ClickEntity.CLICK_ITEM_CHILD_VIEW));
        mAdapter = new ItemClickAdapter(data);
        mAdapter.setAnimationEnable(true);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }
}
